package com.hzdl.cinema.web.servlet;

import com.hzdl.cinema.Service.ScheduleService;

import com.hzdl.cinema.Service.impl.ScheduleServiceImpl;
import com.hzdl.cinema.dao.impl.ScheduleDaoImpl;
import com.hzdl.cinema.domain.Play;
import com.hzdl.cinema.domain.Schedule;
import com.hzdl.cinema.domain.Studio;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/schedule/*")
public class ScheduleServlet extends BaseServlet {
    ScheduleService scheduleService = new ScheduleServiceImpl();

    //查询演出是否开演
    public void findSchedule(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String playName = request.getParameter("playName");
        Schedule schedule = scheduleService.schedule(playName);
        writeValue(response, schedule);
    }

    public void seeSchedule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Schedule> list= scheduleService.seeSchedule();
        writeValue(response,list);

    }

    public void seePlay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String playId = request.getParameter("playId");
//        "select_button":select_button
//        String select_button = request.getParameter("select_button");
        Play play = scheduleService.seePlay(playId);

        writeValue(response,play);
    }

    public void seeStudio(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String studioId = request.getParameter("studioId");
        Studio studio=scheduleService.seeStudio(studioId);
        writeValue(response,studio);
    }
	
	
	 public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Schedule> scheduleList= scheduleService.findAllSchedule();
        writeValue(response,scheduleList);
    }

    public void findScheduleById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int scheduleId = Integer.parseInt(request.getParameter("scheduleId"));
        Schedule schedule= scheduleService.findScheduleById(scheduleId);
        writeValue(response,schedule);
    }

    public void modifySchedule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int scheduleId = Integer.parseInt(request.getParameter("scheduleId"));
        String playName = request.getParameter("playName");
        String studioName = request.getParameter("studioName");
        String time = request.getParameter("startTime");
        String price=request.getParameter("schedulePrice");

        Schedule schedule= new Schedule();
        Play play = new Play();
        Studio studio = new Studio();

        Date startTime=null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            startTime=sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BigDecimal schedulePrice=new BigDecimal(price);

        play.setPlayName(playName);
        studio.setStudioName(studioName);
        schedule.setStartTime(startTime);
        schedule.setScheduleId(scheduleId);
        schedule.setSchedulePrice(schedulePrice);
        schedule.setPlay(play);
        schedule.setStudio(studio);

        scheduleService.modifyScheduleById(schedule);
    }

    public void deleteScheduleById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int scheduleId = Integer.parseInt(request.getParameter("scheduleId"));
        scheduleService.deleteScheduleById(scheduleId);
    }

    public void addSchedule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String playName = request.getParameter("PlayName");
        String studioName = request.getParameter("studioName");
        String time = request.getParameter("startTime");
        String price = request.getParameter("schedulePrice");

        Date startTime=null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            startTime=sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BigDecimal schedulePrice=new BigDecimal(price);

        scheduleService.addSchedule(playName,studioName,startTime,schedulePrice);
    }

    public void searchPlayId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String playName = request.getParameter("playName");
        Integer playId=scheduleService.searchPlayId(playName);
        writeValue(response,playId);
    }

    public void searchSchedule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int playId = Integer.parseInt(request.getParameter("playId"));
        Schedule schedule=scheduleService.searchSchedule(playId);
        writeValue(response,schedule);
    }

}
